package com.google.code.server4j.connector;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Response {

	private String webRoot;
	private OutputStream os;
	private String context;
	
	public void setContext(String context) {
		this.context = context;
	}

	public Response(OutputStream os) {
		this.os = os;
	}
	
	public void setWebRoot(String webRoot) {
		this.webRoot = webRoot;
	}
	
	public void response() throws IOException {
		File file = new File(webRoot,context);
		FileInputStream fis = new FileInputStream(file);
		byte[] buf = new byte[100];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int len = -1;
		while((len=fis.read(buf)) != -1) {
			baos.write(buf,0,len);
		}
		os.write(baos.toByteArray());
		os.flush();
	}
	
	public void response404(String context)throws IOException {
		File file = new File(webRoot,context);
		FileInputStream fis = new FileInputStream(file);
		byte[] buf = new byte[100];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int len = -1;
		while((len=fis.read(buf)) != -1) {
			baos.write(buf,0,len);
		}
		os.write(baos.toByteArray());
		os.flush();
	}
}
